package tool

import (
	"bufio"
	"encoding/json"
	"os"
)

type Config struct {
	AppName      string             `json:"app_name"`
	SearchEngine SearchEngineConfig `json:"search_engine"`
	IPFSAPI      IpfsApiConfig      `json:"ipfsapi"`
}
type SearchEngineConfig struct {
	Host string `json:"host"`
	Port string `json:"port"`
}
type IpfsApiConfig struct {
	Host string `json:"host"`
	Port string `json:"port"`
}

var _cfg *Config

func GetConfig() *Config {
	return _cfg
}

func ParseConfig(path string) (*Config, error) {
	file, err := os.Open(path)
	if err != err {
		panic(err)
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	decoder := json.NewDecoder(reader)
	if err = decoder.Decode(&_cfg); err != nil {
		return nil, err
	}
	return _cfg, nil
}
